<template>
  <el-menu
    :default-active="activeMenu"
    background-color="rgba(51, 55, 68, 1)"
    text-color="#ffffff"
    active-text-color="#ffffff"
    @select="handleSelect"
    :style="theme2"
    style="width: 180px !important; background-color: rgba(51, 55, 68, 1)"
  >
    <template v-for="(item, index) in topMenus">
      <el-menu-item :style="{ '--theme': theme }" :index="item.path" :key="index" v-if="index < visibleNumber" :class="theme3">
        <svg-icon :icon-class="item.meta.icon" />
        {{ item.meta.title }}
      </el-menu-item>
    </template>

    <!-- 顶部菜单超出数量折叠 -->
    <!--    <el-sub-menu :style="{ '&#45;&#45;theme': theme }" index="more" v-if="topMenus.length > visibleNumber" :class="theme4">
      <template #title>更多菜单</template>
      <template v-for="(item, index) in topMenus">
        <el-menu-item :index="item.path" :key="index" v-if="index >= visibleNumber">
          <svg-icon :icon-class="item.meta.icon" />
          {{ item.meta.title }}
        </el-menu-item>
      </template>
    </el-sub-menu>-->
  </el-menu>
</template>

<script setup>
  import { constantRoutes } from '@/router';
  import { isHttp } from '@/utils/validate';
  import useAppStore from '@/store/modules/app';
  import useSettingsStore from '@/store/modules/settings';
  import usePermissionStore from '@/store/modules/permission';
  import { computed, ref } from 'vue';
  import { useRoute, useRouter } from 'vue-router';

  // 顶部栏初始数
  const visibleNumber = ref(15);
  // 当前激活菜单的 index
  const currentIndex = ref(null);
  // 隐藏侧边栏路由
  const hideList = ['/index', '/user/profile', '/tenant/channel/index'];

  const appStore = useAppStore();
  const settingsStore = useSettingsStore();
  const permissionStore = usePermissionStore();
  const route = useRoute();
  const router = useRouter();

  // 主题颜色
  const theme = computed(() => settingsStore.theme);

  const theme2 = computed(() => {
    if ('theme-screen' === settingsStore.theme) {
      return 'background: #1B213B;';
    }
    return 'background: #fff;';
  });

  const theme3 = computed(() => {
    if ('theme-screen' === settingsStore.sideTheme) {
      return 'el-menu-item2';
    }
    return '';
  });

  const theme4 = computed(() => {
    if ('theme-screen' === settingsStore.sideTheme) {
      return 'el-menu-item4';
    }
    return '';
  });
  // 所有的路由信息
  const routers = computed(() => permissionStore.topbarRouters);
  const obj = {
    hidden: false,
    path: '/',
    components: undefined,
    children: [{ path: 'index', component: () => import('@/views/index'), name: 'Index', meta: { title: '首页', icon: 'dashboard', affix: true } }],
  };
  if (routers.value[0].path !== '/') {
    routers.value.unshift(obj);
  }
  const handleOpen = (index) => {
    let pathRoute = routers.value.find((item) => item.path === index);
    if (pathRoute.children !== undefined && pathRoute.children[0].children === undefined) {
      if (pathRoute.children[0].path === '/jin/laiqianle/register/add') {
        router.push('/jin/register');
      } else {
        router.push(pathRoute.children[0].path);
      }
      console.log(4, pathRoute.children[0].path);
    } else if (pathRoute.children !== undefined && pathRoute.children[0].children !== undefined) {
      router.push(pathRoute.children[0].path + '/' + pathRoute.children[0].children[0].path);
    }
  };
  // 顶部显示菜单
  const topMenus = computed(() => {
    let topMenus = [];
    routers.value.map((menu) => {
      if (menu.hidden !== true) {
        // 兼容顶部栏一级菜单内部跳转
        if (menu.path === '/') {
          topMenus.push(menu.children[0]);
        } else {
          topMenus.push(menu);
        }
        // topMenus.push(menu);
      }
    });
    return topMenus;
  });

  // 设置子路由
  const childrenMenus = computed(() => {
    let childrenMenus = [];
    routers.value.map((router) => {
      for (let item in router.children) {
        if (router.children[item].parentPath === undefined) {
          if (router.path === '/') {
            router.children[item].path = '/' + router.children[item].path;
          } else {
            if (!isHttp(router.children[item].path)) {
              router.children[item].path = router.path + '/' + router.children[item].path;
            }
          }
          router.children[item].parentPath = router.path;
        }
        childrenMenus.push(router.children[item]);
      }
    });
    return constantRoutes.concat(childrenMenus);
  });

  // 默认激活的菜单
  const activeMenu = computed(() => {
    const path = route.path;
    let activePath = path;
    // if (path !== undefined && path.lastIndexOf('/') > 0 && hideList.indexOf(path) === -1) {
    if (path !== undefined && path.lastIndexOf('/') > 0 && hideList.indexOf(path) === -1) {
      const tmpPath = path.substring(1, path.length);
      activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/'));
      appStore.toggleSideBarHide(false);
    } else if (!route.children) {
      activePath = path;
      // appStore.toggleSideBarHide(true);
      appStore.toggleSideBarHide(false);
    }
    activeRoutes(activePath);
    // handleOpen(route.path);
    return activePath;
  });

  function setVisibleNumber() {
    const width = document.body.getBoundingClientRect().width / 1.5;
    visibleNumber.value = parseInt(width / 85);
  }

  function handleSelect(key, keyPath) {
    currentIndex.value = key;
    const route = routers.value.find((item) => item.path === key);
    if (isHttp(key)) {
      // http(s):// 路径新窗口打开
      window.open(key, '_blank');
    } else if (!route || !route.children) {
      // 没有子路由路径内部打开
      router.push({ path: key });
      activeRoutes(key);
      // 关闭右侧sidebar
      // appStore.toggleSideBarHide(true);
      // appStore.toggleSideRightBarShow(false);
      appStore.toggleSideBarHide(false);
    } else {
      // 显示左侧联动菜单
      activeRoutes(key);
      handleOpen(key);
      appStore.toggleSideBarHide(false);
      // appStore.toggleSideRightBarShow(true);
    }
  }

  function activeRoutes(key) {
    let routes = [];
    if (childrenMenus.value && childrenMenus.value.length > 0) {
      childrenMenus.value.map((item) => {
        if (key == item.parentPath || (key == 'index' && '' == item.path)) {
          routes.push(item);
        }
      });
    }
    if (routes.length > 0) {
      permissionStore.setSidebarRouters(routes);
    }
    return routes;
  }

  onMounted(() => {
    window.addEventListener('resize', setVisibleNumber);
  });
  onBeforeUnmount(() => {
    window.removeEventListener('resize', setVisibleNumber);
  });

  onMounted(() => {
    setVisibleNumber();
  });
</script>

<style lang="scss">
  .topmenu-container.el-menu--horizontal > .el-menu-item {
    float: left;
    height: 50px !important;
    line-height: 50px !important;
    color: #999093 !important;
    padding: 0 5px !important;
    margin: 0 10px !important;
  }

  .topmenu-container.el-menu--horizontal > .el-menu-item.is-active,
  .el-menu--horizontal > .el-sub-menu.is-active .el-submenu__title {
    border-bottom: 2px solid #{'var(--theme)'} !important;
    color: #303133;
  }

  /* sub-menu item */
  .topmenu-container.el-menu--horizontal > .el-sub-menu .el-sub-menu__title {
    float: left;
    height: 50px !important;
    line-height: 50px !important;
    color: #999093 !important;
    padding: 0 5px !important;
    margin: 0 10px !important;
  }

  .topmenu-container.el-menu--horizontal > .el-menu-item2 {
    float: left;
    height: 50px !important;
    line-height: 50px !important;
    /*color: #999093 !important;*/
    color: #ffffff !important;
    padding: 0 5px !important;
    margin: 0 10px !important;

    &:hover {
      background: #73a7fe !important;
    }

    &:focus {
      background: #73a7fe !important;
    }
  }

  .topmenu-container.el-menu--horizontal > .el-menu-item4 > .el-submenu__title {
    float: left;
    height: 50px !important;
    line-height: 50px !important;
    /*color: #999093 !important;*/
    color: #ffffff !important;
    padding: 0 5px !important;
    margin: 0 10px !important;

    &:hover {
      background: #73a7fe !important;
    }

    &:focus {
      background: #73a7fe !important;
    }
  }
  //:deep(.router-link-active, .router-link-exact-active) {
  //  background-color: rgba(0, 193, 222, 1);
  //}
</style>
<style lang="scss" scoped>
  :deep(.is-active) {
    background-color: rgba(0, 193, 222, 1);
  }
</style>
